﻿Public Class ConnectAuthentication
    Public Sub New()

    End Sub

    Public Shared Function isConnected() As Boolean
        Return (SessionKey IsNot Nothing AndAlso UserID <> -1)
    End Function

    Public Shared ReadOnly Property ApiKey() As String
        Get
            Return ConfigurationManager.AppSettings("APIKey")
        End Get
    End Property

    Public Shared ReadOnly Property SecretKey() As String
        Get
            Return ConfigurationManager.AppSettings("Secret")
        End Get
    End Property

    Public Shared ReadOnly Property SessionKey() As String
        Get
            Return GetFacebookCookie("session_key")
        End Get
    End Property

    Public Shared ReadOnly Property UserID() As Integer
        Get
            'INSTANT VB NOTE: The local variable userID was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property:
            Dim userID_Renamed As Integer = -1
            Integer.TryParse(GetFacebookCookie("user"), userID_Renamed)
            Return userID_Renamed
        End Get
    End Property

    Private Shared Function GetFacebookCookie(ByVal cookieName As String) As String
        Dim retString As String = Nothing
        Dim fullCookie As String = ApiKey & "_" & cookieName

        If HttpContext.Current.Request.Cookies(fullCookie) IsNot Nothing Then
            retString = HttpContext.Current.Request.Cookies(fullCookie).Value
        End If

        Return retString
    End Function
End Class
